package com.zhou.common.mybatis.template.oss.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhou.common.mybatis.template.oss.entity.OssTempFile;
import com.zhou.common.mybatis.template.oss.mapper.OssTempFileMapper;
import com.zhou.common.mybatis.template.oss.model.OssUploadResult;
import com.zhou.common.mybatis.template.oss.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OssService {
    @Autowired
    private OssUtil ossUtil;

    @Autowired
    private OssTempFileMapper ossTempFileMapper;

    @Transactional(rollbackFor = {Exception.class})
    public void service(OssUploadResult ossUploadResult){
        int delete = ossTempFileMapper.delete(Wrappers.<OssTempFile>lambdaQuery().eq(OssTempFile::getObjectKey, ossUploadResult.getObjectKey()));
        if (delete>0){
            ossUtil.deleteObjectKey(ossUploadResult.getObjectKey());
        }
    }
}
